<?php


namespace app\index\controller;


use app\common\controller\Apicommon;
use app\common\controller\Excel;
use think\Db;

class Member extends Apicommon
{
    use Excel;
    public function initialize()
    {
        //parent::initialize();
        header('Access-Control-Allow-Headers: content-type,authorization,token');
        header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
        header('Access-Control-Allow-Origin: *');
        if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
            echo 1;die;
        }
    }

    public function unit()
    {
        $id = input('id');
        $unit = Db::name('device')->where('door_id',$id)->select();
        $this->assign('unit',$unit);
        return $this->fetch('index/unit');
    }
    //人工开柜
    public function admin_open()
    {
        if ($_POST){
            $admin_uid = session('user_auth.uid');
            if (empty($admin_uid)){
                $this->error('请登录','index/index/login');
            }
            $uid = input('uid');
            $gear = input('gear');
            $door = input('door');
            $type = input('type');
            if (empty($type) || empty($door) || empty($gear)){
                return $this->ajaxReturn(1,'必填项未填写');
            }
//            if($type == 2){ // 1安检开柜,2人工开柜
//                if (empty($uid)){
//                    return $this->ajaxReturn(1,'人工开门必须选择员工');
//                }
//            }
            $is_admin = false;
            if (empty($uid)) {
                $is_admin = true;
                $uid = $admin_uid;
            }
            $res = Db::name('admin_record')->insert([
                'admin_uid' => $admin_uid,
                'door'=>$door,
                'type' => $type,
                'uid' => $uid,
                'create_time' => time()
            ]);
            if ($res){
                $dooroperate = new Dooroperate();
                $operation = $type == 2 ? 1 : 2; // 开柜意图，1使用设备,2设备安检,3其他

                // 查询door_id
                $door_id = \app\index\model\Door::where([
                    'gear_id'=>$gear,
                    'door_serial'=>$door,
                ])->value('id');
                if (empty($door_id)) {
                    $this->error('开门失败，柜子信息有误');
                }
                $openn_res = $dooroperate->openDoor($door_id,$uid,$operation,$is_admin);
                if ($openn_res) {
                    return $this->ajaxReturn(0,'开门成功');
                }else{
                    return $this->ajaxReturn(1,$dooroperate->getError());
                }
            }
            return $this->ajaxReturn(1,'开门失败');
        }
        $grade = Db::name('grade')->field('id,grade_name')->select();
        $gear = Db::name('gear')->field('id,gear_serial')->select();
        $door = Db::name('door')->field('id,door_serial')->select();
        $this->assign('grade',$grade);
        $this->assign('door',$door);
        $this->assign('gear',$gear);
        return $this->fetch('index/admin_open');
    }
    public function get_member($grade='')
    {
        $arr = Db::name('member_user')->where('grade_id', $grade)->field('id,username')->select();
       /* foreach ($city as $key => $value) {
            $arr['list'][] = ['key' => $key, 'value' => $value];
        }
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息*/
        return json(['code'=>0,'msg'=>'成功','data'=>$arr]);
    }
    //人工开柜记录
    public function admin_record()
    {
        $page = input('page') ?: 1;
        $where = [];
        $admin_record = Db::view('Admin_record','id,type,door,create_time')
            ->view('Admin_user','username as admin_username','Admin_record.admin_uid = Admin_user.id')
            ->view('Member_user','username','Member_user.id = Admin_record.uid')
            ->where($where)->page($page,10)->order('id desc')->paginate();

        $data = $admin_record->items();
        foreach ($data as $key => $value){
            $data[$key]['door'] = Db::name('door')->where('id',$value['door'])->value('door_serial');
            if ($value['type'] == 1){
                $data[$key]['type'] = '安检开柜';
            }else{
                $data[$key]['type'] = '人工开柜';
            }
        }
        $pages = $admin_record->render();
        $this->assign('admin_record',$data);
        $this->assign('pages',$pages);
        return $this->fetch('index/admin_record');
    }

    public function admin_record_export()
    {
        $admin_record = Db::view('Admin_record','id,type,door,create_time')
            ->view('Admin_user','username as admin_username','Admin_record.admin_uid = Admin_user.id')
            ->view('Member_user','username','Member_user.id = Admin_record.uid')
            ->order('id desc')->select();
        $data = [];
        foreach ($admin_record as $key => $value){
            $temp['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
            $temp['type'] = $value['type'] == 1 ? '安检开柜' : '人工开柜';
            $temp['admin_username'] = $value['admin_username'];
            $temp['username'] = $value['username'];
            $temp['door'] = Db::name('door')->where('id',$value['door'])->value('door_serial');
            $data[] = $temp;
        }
        $this->exportExcel('开柜记录',[
            '操作时间',
            '操作类型',
            '操作管理员',
            '员工姓名',
            '柜体编号'
        ],$data);
    }

    public function grade_add()
    {
        if ($_POST){
            $grade_name = trim(input('grade_name'));
            $leader = trim(input('leader'));
            $mobile = trim(input('mobile'));
            if (empty($grade_name) || empty($leader) || empty($mobile)){
                return $this->ajaxReturn(1,'有必填项未填写');
            }
            $data['grade_name'] = $grade_name;
            $data['leader'] = $leader;
            $data['mobile'] = $mobile;
            $res = Db::name('grade')->insertGetId($data);
            if ($res){
                return $this->ajaxReturn(0,'新增成功');
            }
            return $this->ajaxReturn(1,'新增失败');
        }
       return $this->fetch('index/grade_add.html');
    }

    public function grade_edit()
    {
        $id = input('id');
        if (empty($id)){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $grade_name = trim(input('grade_name'));
        $leader = trim(input('leader'));
        $mobile = trim(input('mobile'));
        if (empty($grade_name) || empty($leader) || empty($mobile)){
            return $this->ajaxReturn(1,'有必填项未填写');
        }
        $data['grade_name'] = $grade_name;
        $data['leader'] = $leader;
        $data['mobile'] = $mobile;
//        dump($data);die;
        $res = Db::name('grade')->where('id',$id)->update($data);
        if ($res){
            return $this->ajaxReturn(0,'新增成功');
        }
        return $this->ajaxReturn(1,'新增失败');
    }

    public function grade_list()
    {
        $grade_name = trim(input('grade_name'));
        $leader = trim(input('leader'));
        $mobile = trim(input('mobile'));
        $where = [];
        if (!empty($grade_name)){
            $where[] = ['grade_name','like','%' . $grade_name . '%'];
        }
        if (!empty($leader)){
            $where[] = ['leader','like','%' . $leader . '%'];
        }
        if (!empty($mobile)){
            $where[] = ['mobile','like','%' . $mobile . '%'];
        }

        $page = input('page') ?: 1;
        $limit = input('limit')?:10;
        $data = Db::name('grade')->where($where)->page($page,$limit)->select();
        if (empty($data)){
            return $this->ajaxReturn(1,'暂无班组');
        }
        $result['data'] = $data;
        $result['count'] = Db::name('grade')->where($where)->count('id');

        return $this->ajaxReturn(0,'班组列表',$result);
    }

    public function grade_del()
    {
        $id = input('id');
        if (empty($id)){
            return $this->ajaxReturn(1,'缺少参数');
        }

        $res = Db::name('grade')->where('id',$id)->delete();
        if ($res){
            return $this->ajaxReturn(0,'删除成功');
        }
        return $this->ajaxReturn(1,'删除失败');
    }
}